package com.storlead.shiro.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.storlead.shiro.model.UserInfo;
import com.storlead.shiro.service.UserInfoService;
import com.storlead.shiro.util.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @Author: Timo
 * @Date: 2019/2/12 11:35
 * @Description: 用户登陆控制器
 */

@Controller
@RequestMapping("/user")
public class UserLoginController {

    @Resource
    private UserInfoService userInfoService;

    @GetMapping("/test")
    public String toLogin() {
        return "login";
    }

    @GetMapping("/login")
    public String login(UserInfo userInfo, Model model) {
        Result result = userInfoService.login(userInfo);
        if (result.isStatus()){
            return "index";
        }
        model.addAttribute("result",result);
        return  "login";
    }

    @ResponseBody
    @GetMapping("/get/{id}")
    public String get(@PathVariable Integer id) {
        UserInfo userInfo = userInfoService.findUser(id);
        return JSON.toJSONString(userInfo);
    }

    @GetMapping("/list")
    public String list() {
        return  "/user/userList";
    }

    @GetMapping("/add")
    public String add() {
        return  "/user/userAdd";
    }

    @GetMapping("/update")
    @RequiresPermissions("user:update")
    public String update() {
        return  "/user/userUpdate";
    }


    @GetMapping("/test_role")
    @RequiresPermissions("user:test_role")
    public String testRole() {
        return  "/user/testRole";
    }
}
